<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * M_login
 *
 * This is an M_login of a few basic login interaction methods you could use
 * all done with a hardcoded array.
 *
 * @package		CodeIgniter
 * @subpackage	Rest Server
 * @category	Controller
 * @author		Phil Sturgeon
 
*/

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH.'/libraries/REST_Controller.php';

class Login extends REST_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->model('mdl_login');
		$this->load->model('mdl_apps');
		$this->load->model('mdl_openwallet');
	}

	
	function check_login_post(){
		               
		//post
		
		$username=$this->post('username'); 
		$password=$this->post('password'); 
		$appsidonapps=$this->post('appsidonapps'); 
		$walletupdateonapps=$this->post('walletupdate'); 
		$langidonapps=$this->post('langid'); 
		$ip=$this->post('ip'); 
		$device=$this->post('device');
		$os=$this->post('os'); 
		$location=$this->post('location'); 
		
		// check login, firsttime, verification, pin
		
		$encrypt=md5($password);
		$arrwhere="username = '" . $username . "' and password = '" . $encrypt . "'";
		$query = $this->mdl_login->get_item(0, 0, $arrwhere);
		$data = '';
		$logincheck = 0;
		$firstimecheck = 0;
		$verificationcheck=0;
		$pincheck = 0;
		$appsidonappscheck = 0;
				
		$loginstatus=0;
		$firstimestatus = 0;
		$verificationstatus=0;
		$pinstatus = 'false';
		$pinnotification = 'false';
		$appsidonserver = '';
		$appsver = '';
		$appsidstatus = 'true';
		$updateappsidstatus ='false';
		$verificationstatus = 'false';
		$verificationnotification = 'false';
		$walletupdate = 'false';
		$walletupdateonserver = 'false';
		$langid = 'false';
		$langidstatus = 'false';
		$lang = 'false';
		$langidonserver ='false';
		// --- start pengecekan login
		if ($query->num_rows() > 0)
		{
			$i=0;
			foreach($query->result_array() as $row)
			{
				
				$rows[$i]['username'] = $row['username'];
				$rows[$i]['password'] = $row['password'];
				$rows[$i]['firsttimestatus'] = $row['firsttimestatus'];
				$rows[$i]['verificationstatus'] = $row['verificationstatus'];
				$rows[$i]['pinstatus'] = $row['pinstatus'];
				$rows[$i]['appsver'] = $row['appsver'];
				$rows[$i]['walletupdate'] = $row['walletupdate'];
				$rows[$i]['langid'] = $row['langid'];
				

				$i++;
			}
		
			$logincheck = 1;
			$firstimestatus = $rows[0]['firsttimestatus'];
			$verificationstatus = $rows[0]['verificationstatus'];
			$pinstatus = $rows[0]['pinstatus'];
			$appsver = $rows[0]['appsver'];
			$walletupdateonserver = $rows[0]['walletupdate'];
			$langidonserver = $rows[0]['langid'];
			
			
		
		}
		// --- end pengecekan login
		
			// login status
	
	if ($logincheck){
			//$this->response(array('status' => 'true','data' => $data),200);
			$loginstatus = 'true';
			$loginnotification = 'false';
			
			// --- start ceks apps
			
			
			
			$arrwhere="appsid = '" . $appsidonapps .  "'";
			$query = $this->mdl_apps->get_item(0, 0, $arrwhere);
		
		
		
		// cek appsid dari apps
		if ($query->num_rows() > 0)
		{
			
			$i=0;
			foreach($query->result_array() as $row)
			{
				$rows[$i]['appsid'] = $row['appsid'];
				$i++;
			}
			$data = array(
				'datas' => $rows
			);
			
			$appsidonappscheck = 1;
		
			
			$appsidstatus = 'true';
			$updateappsidstatus ='false';

			$debug = 'apps id ada di tapps; ';
			$appsid = $appsidonapps;
		}
		
		//--- start kalau appsid dari apps ada di tapps cek ke tusers
		if ($appsidonappscheck){
			$arrwhere="appsid = '" . $appsidonapps . "' and username ='" .$username. "'";
			$query = $this->mdl_login->get_item(0, 0, $arrwhere);
			$data = '';
			$appsidonservercheck = 0;
		
			// cek appsid dari apps ke t usrs
			if ($query->num_rows() > 0)
			{
			
				$i=0;
				foreach($query->result_array() as $row)
				{
				//$rows[$i]['appsid'] = $row['appsid'];
				$rows[$i]['username'] = $row['username'];
				
				
				$i++;
				}
			
				$appsidonservercheck = 1;
				//$appsidonserver = $rows[0]['appsid'];
			
				$data = array(
				'ip' => $ip,
				'device' => $device,
				'os' => $os,
				'location' => $location
				
				);

				$appsidstatus = 'true';
				$updateappsidstatus ='false';

				$query = $this->mdl_login->updateip($username,$data);
		
				$debug = ' appsid ada di tapps, cocok dg username di tuser';
			
			} else {
				$appsidstatus = 'true';
				$updateappsidstatus ='false';
				$query = $this->mdl_login->updateappsid($username,$appsidonapps);
				$debug = ' appsid ada di tapps, ngak cocok dg username di tuser, update appsid di tuser';
	
			} 
		//--- end kalau appsid dari apps ada di tapps cek ke tusers
			
		}else
		//--- start bikin appsid baru
		
		{
			$appsidstatus = 'true';
			$updateappsidstatus ='true';
			$debug = ' appsid ngak ada di tapps atau null, bikin appsid baru di tapps, update appsid di tuser';
	
			$query = $this->mdl_apps->insert();
			$query = $this->mdl_apps->get_lastitem(0, 0);
			$i=0;
			foreach($query->result_array() as $row)
			{
				
				$rows[$i]['appsid'] = $row['appsid'];
				
				$i++;
			}
			$appsid = $rows[0]['appsid'];
			$query = $this->mdl_login->updateappsid($username,$appsid);
			$appsidonapps = $appsid;
		
		
		}
		
				//--- end bikin appsid baru
	
			
			// ----- end ceks apps
			
			
				// verification status	
			
			if ($verificationstatus == 1){
			$verificationstatus = 'true';
			$verificationnotification = 'false';
			} else {
			$verificationstatus = 'false';
			$verificationnotification = 'Verifikasi akun anda belum dilakukan. Silahkan cek email anda untuk klik link verifikasi.';
			}
			
			
						// pin status
			
			if ($pinstatus == 1){
			$pinstatus = 'true';
			$pinnotification = 'false';
			
			}else{
			$pinstatus = 'false';
			$pinnotification = 'belun terdaftar';
			
			}
			
//  lang
		
				if($langidonapps == '') {
			$langid = $langidonserver;
			$arrwhere="langid = '" . $langid  . "'";
			$query = $this->mdl_login->get_item_lang(0, 0, $arrwhere);
			$result = $query->result_array();
			$lang = $result[0]['lang'];
			$lang = unserialize($lang);
			$langidstatus = 'false';
			$langidnotification = 'change to default languange';
			} else {
			
			if($langidonapps !== $langidonserver) {
			
			$lang ='false';
			$langidstatus = 'false';
			} else {
			$langid = $langidonserver;
			$arrwhere="langid = '" . $langid  . "'";
			$query = $this->mdl_login->get_item_lang(0, 0, $arrwhere);
			$result = $query->result_array();
			$lang = $result[0]['lang'];
			$lang = unserialize($lang);
			$langidstatus = 'true';
			$langidnotification = 'change to '.$langidonapps;
			
			}
		
			
			}
			
			// firsttime status
			
			if ($firstimestatus == 1){
			$firstimestatus = 'true';
			}else{
			$firstimestatus = 'false';
			}

		
			

			// apps version
			
			if ($appsver == ''){
			$appsver = 'false';
			}
			
			
		
			
			
			$this->load->helper('jwt_helper');
			$token = array();
			$token['username'] = $username;
			$token['password'] = $password;
			$token['appsidonapps'] = $appsidonapps;
			$token=JWT::encode($token, 'dompeto');
			$resp ='login';
			
		$login[] =array( 
		'resp' => $resp ,
		'token' => $token ,
		'loginstatus' => $loginstatus ,
		'loginnotification' => $loginnotification , 
		'updateappsidstatus' => $updateappsidstatus,
		'appsidstatus' => $appsidstatus ,
		'appsidonapps' => $appsidonapps ,
		'firstimestatus' => $firstimestatus,
		'verificationstatus' => $verificationstatus,
		'verificationnotification' => $verificationnotification ,
		'pinstatus' => $pinstatus,
		'pinnotification' => $pinnotification,
		'appsver' => $appsver,
		'lang' => $lang,
		'langidstatus' =>($langidstatus),
		'langidnotification' => $langidnotification,
		'debug' => $debug
		);
		
			
		
			
			}else{
			
			$login[]=array(
		'loginstatus' => 'false',
		'loginnotification' => 'Login anda gagal',
		'debug' => 'appsid ngak ada di tapps atau null, login false');
	
						
			
			}
		$this->response($login,200);
			
	}
	
	function update_appsid_post(){
		      

		//post
		
		$username=$this->post('username'); 
		$data=$this->post('appsidonapps'); 
         $query = $this->mdl_login->updateappsid($username,$data );
		
	
		
		
		$updateappsid[] = array(
				'resp' => 'updateappsid' ,
				'updateappsidstatus' => 'false',
				'debug' => 'appsid sudah sinkron dg database',
				'appsidonapps' => $data 
				
				);
		
	
		
			 $this->response($updateappsid, 200);	
		
	
	}
  	function check_appstours_post(){
		      

		//post
		$this->load->helper('jwt_helper');
		$appsidonapps=$this->post('appsidonapps'); 
		$username=$this->post('username'); 
		$token = JWT::decode($_POST['token'], 'dompeto');
		$appsidonappstoken= $token->appsidonapps;
        $usernametoken= $token->username;
        	
		$query = $this->mdl_login->updatefirsttime($usernametoken);
		
		
		if($appsidonapps==$appsidonappstoken) {
		
		$appstour[] = array(
				'resp' => 'appstours',
				'appsidonapps' => $appsidonapps ,
				'firsttimestatus' => 'true',
				'slidesize' => '4', 
				'slide1' => 'slide1.html', 
				'slide2' => 'slide2.html',
				'slide3' => 'slide3.html',
				'slide4' => 'slide4.html',
				'debug' => 'tampailan tour html'
		
				);
		
		} else {
			
		$appstour[] = array(
				'resp' => $resp ,
				'appsidonapps' => $appsidonapps ,
				'firsttimestatus' => 'false',
				'debug' => 'appsid salah'
		
				);
		}
	
		
		
		
	 $this->response($appstour, 200);	
			
	
	}
	
	function check_viewtoc_post(){
		      

		//post
		$this->load->helper('jwt_helper');
		$appsidonapps=$this->post('appsidonapps'); 
		$username=$this->post('username'); 
		$token = JWT::decode($_POST['token'], 'dompeto');
		$appsidonappstoken= $token->appsidonapps;
        $usernametoken= $token->username;
        	
		$resp = 'viewtoc';
		
		if($username==$usernametoken) {
		
		$arrwhere="username = '" . $usernametoken  . "'";
		$query = $this->mdl_login->get_item_viewtoc(0, 0, $arrwhere);
		$result = $query->result_array('agreetocstatus');
		$agreetocstatus = $result[0]['agreetocstatus'];
		
		if($agreetocstatus == '0') {
		
		
		$viewtoc[] = array(
				
				'appsidonapps' => $appsidonapps ,
				'toc' => 'toc.html',
				'agreetocstatus' => 'false', 
				'debug' => 'tampilkan toc'
		
				);
				} else {
		
		
				
				$viewtoc[] = array(
				
				'appsidonapps' => $appsidonapps ,
				'toc' => 'false',
				'agreetocstatus' => 'true', 
				'debug' => 'udah pernah agree'
		
				);
				}
		
			} else {
		
		
		
	
		$viewtoc[] = array(
				
				'appsidonapps' => $appsidonapps ,
				'toc' => 'false',
				'agreetocstatus' => 'false', 
				'debug' => 'appsid salah'
		
				);
		}
	
		
		
	 $this->response($viewtoc, 200);		
	
			
	
	}
}